﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace YQGL.Common
{
    public class FileResult : ActionResult
    {
        public FileResult() { }

        /// <summary>
        /// 文件显示名称必须带后缀名
        /// </summary>
        public string FileName;
        /// <summary>
        /// 文件绝对路径
        /// </summary>
        public string FilePaht;

        public override void ExecuteResult(ControllerContext context)
        {
            if (FileName.IndexOf(".") == -1)
            {
                int t = FilePaht.LastIndexOf(".");
                if (t != -1)
                {
                    FileName += FilePaht.Substring(t);
                }
            }


            System.IO.Stream iStream = null;
            //定义缓冲区大小为300k
            int bufferLenth = 300000;
            byte[] buffer = new Byte[300000];
            //文件长度
            int length;
            try
            {
                //打开文件
                iStream = new System.IO.FileStream(FilePaht, System.IO.FileMode.Open,
                System.IO.FileAccess.Read, System.IO.FileShare.Read);
                long dataToRead = iStream.Length;//文件总大小
                context.HttpContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                context.HttpContext.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
                context.HttpContext.Response.AddHeader("content-type", "application/octet-stream");

                //读取字节
                while (dataToRead > 0)
                {
                    context.HttpContext.Response.Clear();
                    bool cc = context.HttpContext.Response.SuppressContent;
                    //判断客户端是否在线
                    if (context.HttpContext.Response.IsClientConnected)
                    {
                        length = iStream.Read(buffer, 0, bufferLenth);
                        context.HttpContext.Response.OutputStream.Write(buffer, 0, length);
                        try
                        {
                            context.HttpContext.Response.Flush();
                            buffer = new Byte[bufferLenth];
                            dataToRead = dataToRead - length;
                        }
                        catch
                        {
                            dataToRead = -1;
                        }
                    }
                    else
                    {
                        dataToRead = -1;
                    }
                }
            }
            catch (Exception ex)
            {
                context.HttpContext.Response.Write("Error : " + ex.Message);
            }
            finally
            {
                if (iStream != null)
                {
                    iStream.Close();
                }
            }
        }

    }
}